<template>
  <div class>
    <el-dialog :title="title" :visible.sync="dialogFormVisible" width="600px" :close-on-click-modal="false" @close="close">
      <el-form ref="form" :model="form" label-width="120px">
        <el-form-item label="员工姓名" :required="true" prop="trueName">
          <el-input v-model="form.trueName" autocomplete="off" placeholder="请输入员工真实姓名"></el-input>
        </el-form-item>
        <el-form-item label="联系方式" :required="true" prop="phone">
          <el-input v-model="form.phone" autocomplete="off" placeholder="请输入联系方式"></el-input>
        </el-form-item>
        <el-form-item label="登录账号" prop="userName" :required="true">
          <el-input v-model="form.userName" autocomplete="off" placeholder="请输入登录账号"></el-input>
        </el-form-item>
        <el-form-item label="登录密码" prop="passWord" :required="true">
          <el-input v-model="form.passWord" type="password" autocomplete="off" placeholder="请输入登录密码"></el-input>
        </el-form-item>
        <el-form-item label="确认密码" :required="true">
          <el-input v-model="passWord" type="password" autocomplete="off" placeholder="请确认登录密码"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="medium" @click="close">取 消</el-button>
        <el-button size="medium" type="primary" @click="save">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { userSave, getUserInfo } from '@/api/setup';

export default {
  name: 'goodlistedit',
  created() {},
  data() {
    return {
      form: {
        userName: '',
        phone: '',
        passWord: '',
        role_id: '',
        trueName: '',
        type: 4
      },
      passWord: '',
      title: '',
      dialogFormVisible: false
    };
  },
  methods: {
    async showEdit(row) {
      if (!row) {
        this.title = '添加';
      } else {
        this.title = '编辑';
        const { data } = await getUserInfo({ id: row.id });
        Object.assign(this.form, data);
        this.passWord = data.passWord;
      }
      this.dialogFormVisible = true;
    },
    setStoreId(storeId){
      this.form.storeId = storeId
    },
    close() {
      this.$emit('close');
    },
    save() {
      if (this.form.passWord == this.passWord) {
        this.$refs['form'].validate(async (valid) => {
          if (valid) {
            const { msg } = await userSave(this.form);
            this.$baseMessage(msg, 'success');
            this.$emit('fetchData');
            this.dialogFormVisible = false;
          } else {
            return false;
          }
        });
      } else {
        this.$baseMessage('两次密码不一致！', 'warning');
      }
    }
  }
};
</script>
